#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <signal.h> 
#include <stdlib.h>       
#include <string.h>
#include <pthread.h>
//#include <sys/sem.h>
#include <errno.h>
#include <sys/ipc.h>
//#include <sys/msg.h>
#include <sys/timeb.h>
#include <sys/socket.h>
//#include <sys/socketvar.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <semaphore.h>
#include <string.h>
#include <termios.h>
#include <linux/input.h>
#include <semaphore.h>
#include <linux/shm.h>
#include <asm/unistd.h>
#include <sys/poll.h>

#include "common.h"

//protoSlaveType
enum _protoSlaveType
{
		_fingerInfo								= 0x00,
		_keyInfo									= 0x01,
};

static int fingerInfo(struct recvData *recvData, struct packageFilter *pFilter);
static int keyInfo(struct recvData *recvData, struct packageFilter *pFilter);

static int fd = -1;

static int finger(int no,int state,int x,int y)
{
		struct solt_data soltdata;
		soltdata.finger = state;
		soltdata.soltno = no;
		soltdata.abs_x  = x;
		soltdata.abs_y  = y;
		
		if( fd == -1 ) return -1;
			
		write(fd,&soltdata,sizeof(struct solt_data));
			
		return 0;
}


static int key(unsigned int key,unsigned int value)
{
		if( fd == -1 ) return -1;
			
		ioctl (fd, key, value);
		
		return 0;
}


void touchKeyServer(struct recvData *recvData)
{
		if( fd == -1 )
		{
			  fd = open("/proc/virtual_touch",O_RDWR|O_NOCTTY);
			  if(fd == -1)
				{
						printf("can not open /proc/virtual_touch !\n");
						return;	
				}
		}
		
		if(recvData == NULL)
		{
				printf("recvData == NULL ! {%s(%d)}\n",__FILE__,__LINE__);
				return;	
		}
	
		struct packageFilter filter;
		int ret = explainPackageHead(recvData, &filter);
		if(ret < 0)
		{
				return;	
		}
		
		switch(filter.protoSlaveType)
		{
				case _fingerInfo:
				{
						fingerInfo(recvData, &filter);
						break;	
				}
				case _keyInfo:
				{
						keyInfo(recvData, &filter);	
						break;
				}
				default:
				{
						printf("filter.protoSlaveType not right ! filter.protoSlaveType = %d\n", filter.protoSlaveType);
						break;	
				}
		}
}

static int fingerInfo(struct recvData *recvData, struct packageFilter *pFilter)
{
		if(pFilter->dataDirection != 0x00)	
		{
				printf("proto 3,0 pFilter->dataDirection error ! pFilter->dataDirection = %d\n", pFilter->dataDirection);
				return -1;	
		}
		
		if(pFilter->dataLen != 10)
		{
				printf("proto 3,0 pFilter->dataLen error ! pFilter->dataLen = %d\n", pFilter->dataLen);
				return -2;	
		}
	
		if(&(recvData->addr) == NULL)
		{
				printf("recvData->addr == NULL !\n");
				return -3;	
		}
	
		int ret;
		unsigned char fingerNumber,status;
		unsigned short x,y;
		unsigned char privateData[4];	
		
		fingerNumber = pFilter->pData[0];
		status = pFilter->pData[1];
		ret = changeByteToUShort(x, pFilter->pData + 2);
		if(ret < 0)
		{
				printf("pFilter->pData + 2 == NULL !\n");
				return -4;	
		}
		ret = changeByteToUShort(y, pFilter->pData + 4);
		if(ret < 0)
		{
				printf("pFilter->pData + 4 == NULL !\n");
				return -5;	
		}
		memcpy(privateData, pFilter->pData + 6, 4);
		//printf("fingerNumber,status,x,y=%d,%d,%d,%d\n", fingerNumber,status,x,y);
		ret = finger((int)fingerNumber, (int)status, (int)x, (int)y);
		if(ret < 0)
		{
				printf("finger function return error ! fingerNumber,status,x,y=%d,%d,%d,%d\n", fingerNumber,status,x,y);	
		}
}

static int keyInfo(struct recvData *recvData, struct packageFilter *pFilter)
{
		if(pFilter->dataDirection != 0x00)	
		{
				printf("proto 3,1 pFilter->dataDirection error ! pFilter->dataDirection = %d\n", pFilter->dataDirection);
				return -1;	
		}
		
		if(pFilter->dataLen != 7)
		{
				printf("proto 3,1 pFilter->dataLen error ! pFilter->dataLen = %d\n", pFilter->dataLen);
				return -2;	
		}
	
		if(&(recvData->addr) == NULL)
		{
				printf("recvData->addr == NULL !\n");
				return -3;	
		}
	
		int ret;
		unsigned short keyCode;
		unsigned char status;
		unsigned char privateData[4];	
		
		ret = changeByteToUShort(keyCode, pFilter->pData + 0);
		if(ret < 0)
		{
				printf("pFilter->pData + 0 == NULL !\n");
				return -4;	
		}
		status = pFilter->pData[2];
		memcpy(privateData, pFilter->pData + 3, 4);
		//printf("keyCode,status=%d,%d\n", keyCode,status);	
		ret = key((unsigned int)keyCode, (unsigned int)status);
		if(ret < 0)
		{
				printf("key function return error ! keyCode,status=%d,%d\n", keyCode,status);	
		}
	
}


